package cn.achao.csr.auth.service.imp;

import cn.achao.csr.api.model.entity.SysUser;
import cn.achao.csr.auth.service.PasswordService;
import cn.achao.csr.base.constant.enums.SysSourceEnum;
import cn.achao.csr.base.exception.BusinessException;
import cn.achao.csr.base.utils.ThrowUtils;
import cn.achao.csr.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: achao
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PasswordServiceImpl implements PasswordService {

    @Override
    public void validate(SysUser user, String userPwd) {
        ThrowUtils.throwIf(!matches(user, userPwd),
                BusinessException.fail("用户名或密码错误", SysSourceEnum.CSR_AUTH.getSysName()));
    }

    public boolean matches(SysUser user, String rawPassword) {
        return SecurityUtils.matchesPassword(rawPassword, user.getUserPwd());
    }
}
